/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameUser;

import Database.CheckConnection;
import Database.UserSuratTable;
import Model.Surat;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author User
 */
public class UserLihatSurat extends JFrame {

    private JTable tableSurat;
    private ArrayList<Surat> data;
    private JTextField textterima;
    private JLabel label1, label2;
    private JButton buttonOk, buttonterima;

    public UserLihatSurat(ArrayList<Surat> data1) {
        this.setTitle("Daftar Surat");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(220, 150, 800, 500);
        this.setLayout(null);
        this.data = data1;
        
        tableSurat = new JTable();
        tableSurat.setBounds(25, 25, 730, 310);
        tableSurat.setModel(new UserSuratTable(data));
        this.add(tableSurat);
        
        label1 = new JLabel("Surat Diterima :");
        label1.setBounds(330, 340, 120, 25);
        this.add(label1);
        
        label2 = new JLabel("Nomor Surat ");
        label2.setBounds(185, 370, 140, 25);
        this.add(label2);
        
        textterima = new JTextField();
        textterima.setBounds(270, 370, 200, 25);
        this.add(textterima);

        buttonterima = new JButton("Terima");
        buttonterima.setBounds(480, 370, 80, 25);
        this.add(buttonterima);
        buttonterima.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(textterima.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "Data belum diisi");
                } else {
                    data = new ArrayList<Surat>();
                    Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                    Statement stat = null;
                    String nomor = textterima.getText();
                    String masuk = "update SURAT set DAFTAR_TERIMA = 'DITERIMA' where ID_PENERIMA in (select DATA from CACHE) and (NO_SURAT = '"+nomor+"')";
                    try {
                        stat = con.createStatement();
                        stat.executeUpdate(masuk);
                    } catch (SQLException ex) {
                        Logger.getLogger(UserLihatSurat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    ResultSet hasil = null;
                    String sql = "select * from SURAT where ID_PENERIMA in (select DATA from CACHE)";
                    try {
                        hasil = stat.executeQuery(sql);
                    } catch (SQLException ex) {
                        Logger.getLogger(UserLihatSurat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        while (hasil.next()) {
                            Surat surat = new Surat();
                            try {
                                surat.setNomor_surat(hasil.getString("NO_SURAT"));
                                surat.setPerihal(hasil.getString("PERIHAL"));
                                surat.setAsal_surat(hasil.getString("ASAL_SURAT"));
                                surat.setPenerima_surat(hasil.getString("PENERIMA_SURAT"));
                                surat.setId_penerima(hasil.getString("ID_PENERIMA"));
                                surat.setLetak_surat(hasil.getString("LETAK_SURAT"));
                                surat.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                                surat.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                                surat.setTerima(hasil.getString("DAFTAR_TERIMA"));
                                data.add(surat);
                            } catch (Exception ex) {
                                Logger.getLogger(UserLihatSurat.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(UserLihatSurat.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    textterima.setText(null);
                    JOptionPane.showMessageDialog(null, "Data Diterima");
                    setVisible(false);
                    UserLihatSurat surat = new UserLihatSurat(data);
                    surat.setVisible(true);
                }
            }
        });

        buttonOk = new JButton("OK");
        buttonOk.setBounds(335, 415, 80, 30);
        this.add(buttonOk);
        buttonOk.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
    
    public ArrayList<Surat> getData() {
        return data;
    }

    public void setData(ArrayList<Surat> data) {
        this.data = data;
    }
}
